<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>客户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/resources/css/public.css" media="all" />
</head>
<body class="childrenBody">
<!-- 查询条件开始-->
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form" id="searchForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">客户姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="custname" id="search_custname" class="layui-input searchVal" placeholder="请输入客户姓名" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">身份证号码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="identity" id="search_identity" class="layui-input searchVal" placeholder="请输入身份证号码" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="startTime" id="startTime" class="layui-input searchVal" readonly placeholder="开始时间" />
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">结束时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="endTime" id="endTime" class="layui-input searchVal" readonly placeholder="结束时间" />
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit lay-filter="doSearch">
                        <span class="layui-icon layui-icon-search"></span>查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-warm" id="resetSearch">
                        <span class="layui-icon layui-icon-refresh-1"></span>重置
                    </button>
                </div>
            </div>
        </form>
    </blockquote>
</form>
<!-- 查询条件结束-->


<!-- 数据表格开始 -->
<div>
    <table class="layui-hide" id="customerTable" lay-filter="customerTable"></table>
    <!-- 工具栏 -->
    <div id="customerToolBar" style="display: none;">
        <button type="button" lay-event="add" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-add-1"></span>添加客户</button>
    </div>

    <div id="customerRowBar" style="display: none;">
        <button type="button" lay-event="update" class="layui-btn layui-btn-sm"><span class="layui-icon layui-icon-edit"></span>更新</button>
        <button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger">
            <span class="layui-icon layui-icon-delete"></span>删除
        </button>
    </div>
</div>
<!-- 数据表格结束 -->

<!-- 添加和修改的弹出层开始 -->
<div  style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form class="layui-form" style="width:80%;" id="dataFrm" lay-filter="dataFrm">
        <!-- 隐藏域，保存客户id -->
        <input type="hidden" name="id">
        <div class="layui-form-item" style="margin-top: 30px;">
            <div class="layui-inline">
                <label class="layui-form-label">客户名称</label>
                <div class="layui-input-block">
                    <input type="text" name="custname" lay-verify="required" autocomplete="off" placeholder="请输入客户名称" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-block">
                    <input type="text" name="phone" lay-verify="required" autocomplete="off" placeholder="请输入联系电话" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">职位</label>
                <div class="layui-input-block">
                    <input type="text" name="career" lay-verify="required" autocomplete="off" placeholder="请输入职位" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="1" title="男" checked>
                    <input type="radio" name="sex" value="0" title="女" >
                </div>
            </div>

        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">身份证号码</label>
            <div class="layui-input-block">
                <input type="text" name="identity" lay-verify="required" autocomplete="off" placeholder="请输入身份证号码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">客户地址</label>
            <div class="layui-input-block">
                <input type="text" name="address" autocomplete="off" placeholder="请输入客户地址" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;">
                <button type="button" class="layui-btn" lay-submit lay-filter="doSubmit" >
                    <span class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm"><span class="layui-icon layui-icon-refresh-1"></span>重置</button>
            </div>
        </div>
    </form>
</div>
<!-- 添加和修改的弹出层结束 -->


<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery','form','table','layer','laydate','laydate'],function () {
        var $=layui.jquery;
        var form=layui.form;
        var table=layui.table;
        var layer=layui.layer;
        var laydate=layui.laydate;

        //初始化时间选择器
        laydate.render({
            elem:'#startTime',//绑定渲染的元素
            type:'datetime'//控件选择类型
        });
        laydate.render({
            elem:'#endTime',//绑定渲染的元素
            type:'datetime'//控件选择类型
        });

        tableIns=table.render({
            elem: '#customerTable'
            ,url:'/bus/customer/list'
            ,toolbar: '#customerToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,page: true
            ,title: '客户数据表'
            ,cols: [  [
                {type:'checkbox'}
                ,{field:'id',title:'用户编号',align:'center'}
                ,{field:'custname',title:'客户名称',align:'center'}
                ,{field:'phone',title:'联系电话',align:'center'}
                ,{field:'career',title:'职位',align:'center'}
                ,{field:'sex',title:'性别',align:'center',templet:function (d) {
                        return d.sex==1?"<font color='blue'>男</font>":"<font color='red'>女</font>";
                    }}
                ,{field:'identity',title:'身份信息',align:'center'}
                ,{field:'address',title:'地址',align:'center'}
                ,{field:'createtime',title:'创建时间',align:'center'}
                ,{title:'操作',toolbar:'#customerRowBar'}
            ]  ],
            done:function (res,curr,count) {
                //判断当前页面是否大于1并且当前页的数据为0
                if (curr>1&&res.data.length==0){
                    var pageValue=curr-1
                    //刷新数据表格
                    tableIns.reload({
                        page:{curr:pageValue}
                    });
                }
            }
        });

        //监听表单提交事件
        form.on("submit(doSearch)",function (data) {
            tableIns.reload({
                url:'/bus/customer/list',
                where:data.field,
                page:{
                    curr:1
                }
            });
            return false;
        });



        //监听头部工具栏事件
        table.on('toolbar(customerTable)',function (obj) {
            switch (obj.event) {
                case 'add':
                    //打开头部工具栏添加公告的窗口
                    openAddCustomer();
                    break;
            }
        })


        //监听行工具栏事件
        table.on('tool(customerTable)',function (obj) {
            switch (obj.event) {
                case 'update':
                    //打开头部工具栏添加公告的窗口
                    updateCustomer(obj.data);
                    break;
                case 'delete':
                    deleteById(obj.data);
                    break
            }
        })


        var mainIndex;//打开窗口的索引
        var url;//提交请求的地址

        /**
         * 添加部门
         */
        function openAddCustomer(){
            mainIndex= layer.open({
                type:1,
                content:$('#addOrUpdateDiv'),
                area:["800px","500px"],
                title:"添加客户",
                success:function () {
                    //每次打开窗口，清空表单数据
                    $("#dataFrm")[0].reset();
                    //提交数据
                    url="/bus/customer/addCustomer";
                }
            })
        }

        //监听表单提交事件
        form.on("submit(doSubmit)",function (data) {
            $.post(url, data.field, function (result) {
                if (result.success){
                    //提示信息
                    layer.msg(result.message,{icon:1})

                   tableIns.reload();

                    layer.close(mainIndex);

                }else {
                    layer.msg(result.message,{icon:2});
                }
            }, "json");
            //禁止页面跳转
            return false;
        })

        /**
         * 修改客户
         */
        function  updateCustomer(data){
            mainIndex= layer.open({
                type:1,
                content:$('#addOrUpdateDiv'),
                area:["800px","500px"],
                title:"修改客户",
                success:function () {
                    //表单数据回显
                    form.val("dataFrm",data);
                    //提交数据
                    url="/bus/customer/updateCustomer";
                }

            })
        }

        /**
         * 删除
         */
        function deleteById(data){
            layer.confirm('是否要删除该数据？', {"icon":3,"title":"提示"},function (index) {
                $.post("/bus/customer/deleteById", {"id":data.id}, function (result) {
                    if (result.success){
                        tableIns.reload();//表格重新加载
                    }
                    layer.msg(result.message);
                }, "json");
                layer.close(index);
            });

        }


    });
</script>
</body>
</html>